package com.kevin.util;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

import java.io.IOException;
import java.util.Arrays;

public class HttpClientExample {

    public static String sendPostRequest(String url, String requestBody) throws IOException {
        CloseableHttpClient httpClient = HttpClients.createDefault();
        HttpPost httpPost = new HttpPost(url);

        // 设置请求体
        StringEntity requestEntity = new StringEntity(requestBody, ContentType.APPLICATION_JSON);
        httpPost.setEntity(requestEntity);
        //请求体加一个cookie
        httpPost.setHeader("Cookie", "\n" +
                "SESSION=79b5a539-9c72-4e12-8162-cac6ab58e4c4; BIGipServerNCS_Nginx_P80=!Q9Y9c5t5QRo6sNNlxDsjQ5eIaE4M2V3S+wYUsXy/u9GvTjflXQfKfN3ceeHt05++v0lA/UrGH7dut1A=; _pk_id.19.92e3=d7627426922d62d2.1735267384.; UAMSessionID=E9C9B875667E5DDE1C8C56DB61041953.instance-74-25; UAMAuth=89979dc778eff37501798dedb396660f; UAMauthentication=010db9dc-deac-4d59-a6b6-c1bddf6a90ad; BIGipServerPool_SHDC_Portol_Auth2=!Do0aJgJhhgfg92qL3UZgqG4W5Y/lORl7AT/dBCruVCTc9rrWRZdjBiNTStuvkwU558e6sCRvLi5jbg==; BIGipServerPool_Matomo_T033_P8000=!/wHXdZk4z34HTXplxDsjQ5eIaE4M2Swhu8m0JOpEC6KDwULuUD3bBBCHX189875inTGLFg1hMP/eVmM=; AlteonPP7001=!3DNzgBVKOyQJF0s2uevH6SUSwvXI3yH5ErVJuPXFvUnZ4jRzuxnPL6+L0JMU28nmHd2H+qcMvF9qQMI=; JSESSIONID=318ADDCE15FFC79F1016549DE24D6345.instance-53-101; _pk_ses.19.92e3=1");


        // 发送请求并获取响应
        HttpResponse httpResponse = httpClient.execute(httpPost);
        HttpEntity responseEntity = httpResponse.getEntity();

        // 解析响应
        String response = EntityUtils.toString(responseEntity);

        // 关闭资源
        EntityUtils.consume(responseEntity);
        httpClient.close();

        return response;
    }
}
